<template>
    <div class="basic_setting_page">
        <div class="content-header">
            <el-row type="flex" class="row-bg" justify="space-between" style="padding: 10px 10px">
                <el-col :span="6" class="header-title">系统设置</el-col>
                <el-col :span="6">
                    <div style="text-align: right">
                        <el-button-group>
<!--                            <el-button type="primary" plain size="mini" icon="el-icon-circle-plus-outline"-->
<!--                                       @click="dialogShow=true">同步配置-->
<!--                            </el-button>-->
                        </el-button-group>
                    </div>
                </el-col>
            </el-row>
        </div>

        <el-tabs type="card"  >
            <el-tab-pane label="基础设置" style="padding-top: 10px">
                <el-form label-width="100px" label-position="right">
                    <el-form-item size="small" label="登录认证:">
                        <el-radio-group v-model="base.auth_way">
                            <el-radio label="user_pass">用户名+密码</el-radio>
                            <el-radio label="user_pass_id">用户名+设备</el-radio>
                            <el-radio label="id">仅设备</el-radio>
                        </el-radio-group>
                    </el-form-item>

                    <el-form-item size="small" label="掉线处理:">
                        <el-checkbox-group v-model="base.offline">
                            <el-checkbox label="save">保存记录</el-checkbox>
                            <el-checkbox label="publish">发布掉线消息</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>

                    <el-form-item size="small">
                        <el-row>
                            <el-col :span="2" style="text-align: center">
                                <el-button type="primary"  @click="saveBase" size="small">保 存</el-button>
                            </el-col>
                        </el-row>
                    </el-form-item>
                </el-form>

            </el-tab-pane>
            <el-tab-pane label="推送设置" style="padding-top: 10px">
                <el-form label-width="100px" label-position="right">
                    <el-form-item size="small" label="消息质量:">
                        <el-radio-group v-model="push.push_qos">
                            <el-radio label="0">Qos0</el-radio>
                            <el-radio label="1">Qos1</el-radio>
                            <el-radio label="2">Qos2</el-radio>
                        </el-radio-group>
                    </el-form-item>

                    <el-form-item size="small" label="是否记录:">
                        <el-radio-group v-model="push.push_save">
                            <el-radio label="1">创建</el-radio>
                            <el-radio label="0">不创建</el-radio>
                        </el-radio-group>
                    </el-form-item>

                    <el-form-item size="small">
                        <el-row>
                            <el-col :span="2" style="text-align: center">
                                <el-button type="primary"  @click="savePush" size="small">保 存</el-button>
                            </el-col>
                        </el-row>
                    </el-form-item>
                </el-form>

            </el-tab-pane>

            <el-tab-pane label="消息设置" style="padding-top: 10px">
                <el-form label-width="100px" label-position="right">
                    <el-form-item size="small" label="离线消息:">
                        <el-radio-group v-model="message.save_undelivred">
                            <el-radio label="save">保存</el-radio>
                            <el-radio label="delete">不保存</el-radio>
                        </el-radio-group>
                    </el-form-item>

                    <el-form-item size="small" v-if="message.save_undelivred=='save'" label="保存消息:">
                        <el-checkbox-group v-model="message.save_type">
                            <el-checkbox label="1">Qos1</el-checkbox>
                            <el-checkbox label="2">Qos2</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>

                    <el-form-item size="small" label="最大长度:" >
                        <el-input v-model="message.max_length" type="number"/>
                    </el-form-item>

                    <el-form-item size="small">
                        <el-row>
                            <el-col :span="2" style="text-align: center">
                                <el-button type="primary"  @click="savePush" size="small">保 存</el-button>
                            </el-col>
                        </el-row>
                    </el-form-item>
                </el-form>

            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
    import {get_baseSetting, get_pushSetting, save_baseSetting, save_pushSetting} from "../api/himq_http";
    export default {
        name: "AdminSetting",
        data(){
            return{
                base:{
                    auth_way:'user_pass',
                    offline:["publish","save"],
                },
                push:{
                    push_qos:'1',
                    push_save:'0'
                },
                message:{
                    save_undelivred:'save',
                    save_type:['1','2'],
                    max_length:1024,
                }
            }
        },
        created() {
            this.getPush();
            this.getBase();
        },

        methods:{
            saveBase(){
                let that = this;
                save_baseSetting(this.base).then(
                    resp => {
                        that.$toast({message: resp.message,type:"success"})
                        that.getBase()
                    }, err => {
                        that.$toast({message: err.message,type:"warning"})
                        console.log(err.message)
                    }
                )
            },
            getBase(){
                let that = this;
                get_baseSetting({}).then(
                    resp => {
                        that.base = resp.data
                    }, err => {
                        console.log(err.message)
                    }
                )
            },
            savePush(){
                let that = this;
                save_pushSetting(this.push).then(
                    resp => {
                        that.$toast({message: resp.message,type:"success"})
                        that.getPush()
                    }, err => {
                        that.$toast({message: err.message,type:"warning"})
                        console.log(err.message)
                    }
                )
            },
            getPush(){
                let that = this;
                get_pushSetting({}).then(
                    resp => {
                        that.push = resp.data
                    }, err => {
                        console.log(err.message)
                    }
                )
            },

        }
    }
</script>
<style lang="less">
    .basic_setting_page {
        min-height: 400px;
        background-color: white
    }
    .el-tabs--left .is-left .el-tabs__item:last-child {
        padding-right: 16px !important;
    }

</style>